package com.ygk.bluepath.core;

import android.content.IntentFilter;
import android.widget.TextView;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.exception.DbException;
import com.ygk.bluepath.R;
import com.ygk.bluepath.bean.LocationBean;
import com.ygk.bluepath.bean.LocationBeanCache;
import com.ygk.bluepath.core.View.RecorderActivity;
import com.ygk.bluepath.notification.receiver.GpsReceiver;

import org.json.JSONObject;
import java.util.HashMap;
import java.util.List;

import utils.Inf;
import utils.SharedPreUtil;

/**
 * Created by USER on 2016/7/8.
 */
public class BPresenter {
    protected BActivity activity;
    protected BView mView;
    protected BModel mModel;
    protected SharedPreUtil sp;

    // 自定义定位
    private GpsReceiver dataReceiver;// BroadcastReceiver对象

    public BPresenter(BActivity activity, BView mView) {
        this.activity = activity;
        this.mView = mView;
        sp = activity.sp;
    }

    public BModel getModel() {
        return mModel;
    }

    public void setModel(BModel mModel) {
        this.mModel = mModel;
    }

    public BActivity getActivity() {
        return activity;
    }

    public void setActivity(BActivity activity) {
        this.activity = activity;
    }

    public BView getmView() {
        return mView;
    }

    public void setmView(BView mView) {
        this.mView = mView;
    }

    public void doPost(String url,
                       HashMap<String, Object> bMap, boolean canCancel,
                       boolean showLoading, String oid, Object extraStr) {
        mModel.doPost(url, bMap, canCancel, showLoading, oid, extraStr);
    }

    public void dealJsonObject(JSONObject result, String oid, Object extraStr) {
        // TODO Auto-generated method stub

    }

    public void tt(String s) {
        activity.tt(s);
    }

    public void setViewTiTle(String title) {
        TextView tv = (TextView) activity.findViewById(R.id.tv_title);
        if (tv != null) {
            tv.setText(title);
        }
    }

    public List<?> getLocationAll(Class<?> locationBeanClass) {
        try {
            List<?> mlist = mModel.getLocationDb().findAll(locationBeanClass);
            return mlist;
        } catch (DbException e) {
            e.printStackTrace();
        }

        return null;
    }
    public List<?> getLocationBySelector(Selector sel) {

        try {
            List<?> mlist=  mModel.getLocationDb().findAll(sel);
            return  mlist;
        } catch (DbException e) {
            e.printStackTrace();
        }

        return null;
    }
    public LocationBean getLocationById(int id) {

        try {
            LocationBean bean = (LocationBean) mModel.getLocationDb().findById(LocationBean.class, id);
            return bean;
        } catch (DbException e) {
            e.printStackTrace();
        }

        return null;
    }

    public void deleteById(Class<?> type ,int id) {
        try {
           mModel.getLocationDb().deleteById(type, id);
        } catch (DbException e) {
            e.printStackTrace();
            tt("删除失败");
        }
    }

    public void saveLocation(LocationBean locationBean) {
        try {
            mModel.getLocationDb().save(locationBean);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }
    public void saveLocationCache(LocationBeanCache locationBeanCache) {
        try {
            mModel.getLocationDb().save(locationBeanCache);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public void saveLocationOrUpdate(LocationBeanCache locationBeanCache) {
        try {
            mModel.getLocationDb().saveOrUpdate(locationBeanCache);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }
    public void deleteAll(Class<?> bean) {
        try {
            mModel.getLocationDb().deleteAll(bean);

        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public void reGisterReceiver(BActivity activity) {
        if (dataReceiver == null) {
            dataReceiver = new GpsReceiver(this);
            IntentFilter filter = new IntentFilter();// 创建IntentFilter对象
            filter.addAction(Inf.actionLocationGet);
            activity.registerReceiver(dataReceiver, filter);// 注册BroadcastReceiver
        }
    }
    public void unregisterReceiver() {
        if (dataReceiver != null) {
            activity.unregisterReceiver(dataReceiver);// 取消注册BroadcastReceiver
            dataReceiver = null;
        }
    }

    public void LBSResult(double longitude,double latitude) {//定位结果

    }


}
